package com.glodon.paas.job.model;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.glodon.paas.job.exception.JsonException;
import com.glodon.paas.job.util.JsonUtil;

@Entity
@Table(name = "monitor_status", uniqueConstraints = { @UniqueConstraint(name = "uk_type_trace_id", columnNames = {
        "type", "trace_id", "trace_index" }) })
public class MonitorStatus {
    private long id;
    private String type;
    private String traceId;
    private Integer traceIndex;
    private String cmd;
    //add taskid
    private String taskId;
    private Date createdTime;
    private Date updatedTime;
    private Map<String, Object> values = new HashMap<String, Object>();

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Column(name = "trace_id")
    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    @Column(name = "trace_index")
    public Integer getTraceIndex() {
        return traceIndex;
    }

    public void setTraceIndex(Integer traceIndex) {
        this.traceIndex = traceIndex;
    }

    public String getCmd() {
        return cmd;
    }

    public void setCmd(String cmd) {
        this.cmd = cmd;
    }

    @Column(name = "created_time")
    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    @Column(name = "updated_time")
    public Date getUpdatedTime() {
        return updatedTime;
    }

    public void setUpdatedTime(Date updatedTime) {
        this.updatedTime = updatedTime;
    }

    @Transient
    public Map<String, Object> getValues() {
        return values;
    }

    public void setValues(Map<String, Object> values) {
        this.values = values;
    }

    @Column(columnDefinition = "text")
    public String getJson() {
        return JsonUtil.mapToJson(values);
    }

    public void setJson(String json) throws JsonException {
        values = JsonUtil.jsonToMap(json);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    public String getValue(String key) {
        Object value = values.get(key);
        return value == null ? null : value.toString();
    }

    @Column(name = "task_id")
    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }
}
